#!/usr/bin/bash
# -*- sh -*-

: << =cut

=head1 NAME

paging_in - Plugin to monitor CPU usage

=head1 CONFIGURATION

No configuration

=head1 AUTHOR

Unknown author

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

. $MUNIN_LIBDIR/plugins/plugin.sh

if [ "$1" = "autoconf" ]; then
	if [ -x /usr/bin/kstat ]; then
		echo yes
		exit 0
	else
		echo no
		exit 0
	fi
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Paging In'
	echo 'graph_order pgin pgpgin reclaim'
	echo "graph_args --base 1000"
	echo 'graph_category system'
	echo 'pgin.label pgin'
	echo 'pgin.draw LINE2'
	echo 'pgin.type DERIVE'
	echo 'pgin.min 0'
	echo 'pgin.max 1000000000'
	print_warning pgin
	print_critical pgin
	echo 'pgpgin.label pgpgin'
	echo 'pgpgin.draw LINE2'
	echo 'pgpgin.type DERIVE'
	echo 'pgpgin.min 0'
	echo 'pgpgin.max 1000000000'
	print_warning pgpgin
	print_critical pgpgin
	echo 'reclaim.label reclaim'
	echo 'reclaim.draw LINE2'
	echo 'reclaim.type DERIVE'
	echo 'reclaim.min 0'
	echo 'reclaim.max 1000000000'
	print_warning reclaim
	print_critical reclaim
	exit 0
fi

kstat -p -c misc -m cpu_stat -s '/^(pgin|pgpgin|pgf?rec)$/' | sed -e 's/.*://' | awk '
BEGIN {
	map["pgin"] = "pgin"
	map["pgpgin"] = "pgpgin"
	map["pgrec"] = "reclaim"
	map["pgfrec"] = "reclaim"
}
length(map[$1]) > 0 {
	sum[map[$1]] += $2
}
END {
	for (item in sum) {
		print item ".value", sum[item]
	}
}'
